<?php
namespace app\bz\controller;
use think\Db;
class Order extends Base
{
    private $tn = 'order';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];
            if (isset($this->get['account'])) {
                $where['account'] = ['like', '%' . $this->get['account'] . '%'];
            }            
if (isset($this->get['nickname'])) {
                $where['nickname'] = ['like', '%' . $this->get['nickname'] . '%'];
            }            
            if (isset($this->get['goods'])) {
                $where['g.title'] = ['like',"%". $this->get['goods']."%"];
            }  
if (isset($this->get['status'])) {
                $where['status'] = ['eq', $this->get['status']];
            }      
            $list  = Db::table('bz_shop_order')->alias('o')->join(['bz_shop_goods'=>'g'],'g.id = o.goods_id','left')->join(['bz_user'=>'u'],'u.id=o.user_id','left')->field('o.*,g.title as goods_id,u.nickname as user_id')->where($where)->order('id', 'desc')->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach ($list as $k => $v) {
                switch ($v['status']) {
                case 0:$list[$k]['status'] = '未支付';break;
                case 1:$list[$k]['status'] = '已支付';break;
                case 2:$list[$k]['status'] = '待收货';break;
                case 3:$list[$k]['status'] = '已收货';break;
                case 4:$list[$k]['status'] = '已取消';break;
                default:
                    break;
                }
                $list[$k]['add_time'] = date('y-m-d H:i:s',$v['add_time']);
                if($v['pay_time']>0)
                $list[$k]['pay_time'] = date('y-m-d H:i:s',$v['pay_time']);
                $list[$k]['address'] = Db::table('bz_address')->where('id',$v['address'])->value('add_ress');
            }
            $count = Db::table('bz_shop_order')->alias('o')->join(['bz_shop_goods'=>'g'],'g.id = o.goods_id','left')->join(['bz_user'=>'u'],'u.id=o.user_id','left')->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch();
    }

    public function edit($id = 0)
    {
        $model = model($this->tn);
        if ($this->post) {
            $this->post['express_time'] = time();
            $this->post['status']=2;
            if ($id) {
                $model = $model->get($id);
                if (is_null($model)) {
                    $this->returnAPI('信息有误,请稍后重试');
                }
            }
            $result = $model->validate($this->tn)->allowField(true)->save($this->post);
            if ($result !== false) {
                $this->returnAPI('操作成功', 0);
            }
            $this->returnAPI($model->getError());
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }
        return $this->fetch('', ['data' => $data]);
    }
}
